package com.hjt.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjt.gulimall.product.entity.BrandEntity;
import com.hjt.gulimall.product.vo.BrandResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hjt.gulimall.product.entity.CategoryBrandRelationEntity;
import com.hjt.gulimall.product.service.CategoryBrandRelationService;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author huangjingtai
 * @email 970075985@qq.com
 * @date 2021-09-08 16:15:19
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam("brandId") Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> entityList = categoryBrandRelationService.list(wrapper);

        return R.ok().put("data", entityList);
    }

    /**
     * 获取某个分类关联的所有品牌
     * @return
     */
    @GetMapping("/brands/list")
    public R getRelationBrandList(@RequestParam("catId") Long catId) {
        List<BrandEntity> brandList = categoryBrandRelationService.getBrandListByCatId(catId);
        List<BrandResponseVo> responseVoList =  brandList.stream().map(brandEntity -> {
                BrandResponseVo responseVo = new BrandResponseVo();
                responseVo.setBrandId(brandEntity.getBrandId());
                responseVo.setBrandName(brandEntity.getName());
                return  responseVo;
            }).collect(Collectors.toList());
        return R.ok().put("data", responseVoList);
    }
}
